package test.minio

import io.minio.BucketExistsArgs
import io.minio.MinioClient
import org.junit.jupiter.api.Test

import io.minio.UploadObjectArgs

import io.minio.MakeBucketArgs
import io.minio.PutObjectArgs

class MinioTest {

    private val minioClient = MinioClient.builder()
        .credentials("admin", "password")
        .endpoint("http://localhost:9000")
        .build()

    private val bucketName = "lik"

    @Test
    fun makeBucket() {
        println(minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()))

        // Make 'lik' bucket if not exist.
        val found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())
        if (!found) {
            // Make a new bucket called 'asiatrip'.
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build())
        } else {
            println("Bucket 'lik' already exists.")
        }
    }

    @Test
    fun listBuckets() {
        minioClient.listBuckets().forEach { println("${it.creationDate()} --- ${it.name()}") }
    }

    @Test
    fun `Uploads data from a stream to an object`() {
        // Create a InputStream for object upload.
        val builder = StringBuilder()
        builder.append(
            "Sphinx of black quartz, judge my vow: Used by Adobe InDesign to display font samples. "
        );
        builder.append("(29 letters)\n");

        val bais = builder.toString().byteInputStream()
        // Create object 'my-objectname' in 'my-bucketname' with content from the input stream.
        minioClient.putObject(
            PutObjectArgs.builder().bucket(bucketName)
                .`object`("a/b/c/my-objectname.txt").stream(bais, bais.available().toLong(), -1)
                .build()
        )
        bais.close()
        println("my-objectname is uploaded successfully")
    }

    @Test
    fun `Uploads data from a file`() {
        minioClient.uploadObject(
            UploadObjectArgs.builder()
                .bucket(bucketName)
                .`object`("README.md")
                .filename("D:\\code\\gitee\\python_study\\README.md")
                .build()
        )
        println("successfully uploaded")
    }
}